package cn.qqtheme.framework.util;

import android.webkit.MimeTypeMap;
import cn.qqtheme.framework.adapter.FileAdapter;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.regex.Pattern;

/* loaded from: classes.dex */
public final class FileUtils {
    public static final int BY_EXTENSION_ASC = 6;
    public static final int BY_EXTENSION_DESC = 7;
    public static final int BY_NAME_ASC = 0;
    public static final int BY_NAME_DESC = 1;
    public static final int BY_SIZE_ASC = 4;
    public static final int BY_SIZE_DESC = 5;
    public static final int BY_TIME_ASC = 2;
    public static final int BY_TIME_DESC = 3;

    @Retention(RetentionPolicy.SOURCE)
    /* loaded from: classes.dex */
    public @interface SortType {
    }

    public static String separator(String str) {
        String str2 = File.separator;
        String replace = str.replace("\\", str2);
        if (replace.endsWith(str2)) {
            return replace;
        }
        return replace + str2;
    }

    public static void closeSilently(Closeable closeable) {
        if (closeable == null) {
            return;
        }
        try {
            closeable.close();
        } catch (IOException e) {
            LogUtils.warn(e);
        }
    }

    public static File[] listDirs(String str, String[] strArr, int i) {
        LogUtils.verbose(String.format("list dir %s", str));
        ArrayList arrayList = new ArrayList();
        File file = new File(str);
        if (file.isDirectory()) {
            File[] listFiles = file.listFiles(new FileFilter() { // from class: cn.qqtheme.framework.util.FileUtils.1
                @Override // java.io.FileFilter
                public boolean accept(File file2) {
                    return file2 != null && file2.isDirectory();
                }
            });
            if (listFiles == null) {
                return new File[0];
            }
            if (strArr == null) {
                strArr = new String[0];
            }
            for (File file2 : listFiles) {
                File absoluteFile = file2.getAbsoluteFile();
                if (!ConvertUtils.toString(strArr).contains(absoluteFile.getName())) {
                    arrayList.add(absoluteFile);
                }
            }
            if (i == 0) {
                Collections.sort(arrayList, new SortByName());
            } else if (i == 1) {
                Collections.sort(arrayList, new SortByName());
                Collections.reverse(arrayList);
            } else if (i == 2) {
                Collections.sort(arrayList, new SortByTime());
            } else if (i == 3) {
                Collections.sort(arrayList, new SortByTime());
                Collections.reverse(arrayList);
            } else if (i == 4) {
                Collections.sort(arrayList, new SortBySize());
            } else if (i == 5) {
                Collections.sort(arrayList, new SortBySize());
                Collections.reverse(arrayList);
            } else if (i == 6) {
                Collections.sort(arrayList, new SortByExtension());
            } else if (i == 7) {
                Collections.sort(arrayList, new SortByExtension());
                Collections.reverse(arrayList);
            }
            return (File[]) arrayList.toArray(new File[arrayList.size()]);
        }
        return new File[0];
    }

    public static File[] listDirs(String str, String[] strArr) {
        return listDirs(str, strArr, 0);
    }

    public static File[] listDirs(String str) {
        return listDirs(str, null, 0);
    }

    public static File[] listDirsAndFiles(String str, String[] strArr) {
        File[] listFiles;
        File[] listDirs = listDirs(str);
        if (strArr == null) {
            listFiles = listFiles(str);
        } else {
            listFiles = listFiles(str, strArr);
        }
        if (listDirs == null || listFiles == null) {
            return null;
        }
        File[] fileArr = new File[listDirs.length + listFiles.length];
        System.arraycopy(listDirs, 0, fileArr, 0, listDirs.length);
        System.arraycopy(listFiles, 0, fileArr, listDirs.length, listFiles.length);
        return fileArr;
    }

    public static File[] listDirsAndFiles(String str) {
        return listDirsAndFiles(str, null);
    }

    public static File[] listFiles(String str, final Pattern pattern, int i) {
        LogUtils.verbose(String.format("list file %s", str));
        ArrayList arrayList = new ArrayList();
        File file = new File(str);
        if (file.isDirectory()) {
            File[] listFiles = file.listFiles(new FileFilter() { // from class: cn.qqtheme.framework.util.FileUtils.2
                @Override // java.io.FileFilter
                public boolean accept(File file2) {
                    if (file2 == null || file2.isDirectory()) {
                        return false;
                    }
                    Pattern pattern2 = pattern;
                    if (pattern2 == null) {
                        return true;
                    }
                    return pattern2.matcher(file2.getName()).find();
                }
            });
            if (listFiles == null) {
                return new File[0];
            }
            for (File file2 : listFiles) {
                arrayList.add(file2.getAbsoluteFile());
            }
            if (i == 0) {
                Collections.sort(arrayList, new SortByName());
            } else if (i == 1) {
                Collections.sort(arrayList, new SortByName());
                Collections.reverse(arrayList);
            } else if (i == 2) {
                Collections.sort(arrayList, new SortByTime());
            } else if (i == 3) {
                Collections.sort(arrayList, new SortByTime());
                Collections.reverse(arrayList);
            } else if (i == 4) {
                Collections.sort(arrayList, new SortBySize());
            } else if (i == 5) {
                Collections.sort(arrayList, new SortBySize());
                Collections.reverse(arrayList);
            } else if (i == 6) {
                Collections.sort(arrayList, new SortByExtension());
            } else if (i == 7) {
                Collections.sort(arrayList, new SortByExtension());
                Collections.reverse(arrayList);
            }
            return (File[]) arrayList.toArray(new File[arrayList.size()]);
        }
        return new File[0];
    }

    public static File[] listFiles(String str, Pattern pattern) {
        return listFiles(str, pattern, 0);
    }

    public static File[] listFiles(String str) {
        return listFiles(str, null, 0);
    }

    public static File[] listFiles(String str, final String[] strArr) {
        LogUtils.verbose(String.format("list file %s", str));
        return new File(str).listFiles(new FilenameFilter() { // from class: cn.qqtheme.framework.util.FileUtils.3
            @Override // java.io.FilenameFilter
            public boolean accept(File file, String str2) {
                return ConvertUtils.toString(strArr).contains(FileUtils.getExtension(str2));
            }
        });
    }

    public static File[] listFiles(String str, String str2) {
        return listFiles(str, new String[]{str2});
    }

    public static boolean exist(String str) {
        return new File(str).exists();
    }

    public static boolean delete(File file, boolean z) {
        boolean z2 = true;
        LogUtils.verbose(String.format("delete file %s", file.getAbsolutePath()));
        if (file.isFile()) {
            return deleteResolveEBUSY(file);
        }
        File[] listFiles = file.listFiles();
        if (listFiles == null) {
            return false;
        }
        if (listFiles.length != 0) {
            boolean z3 = false;
            for (File file2 : listFiles) {
                delete(file2, z);
                z3 = deleteResolveEBUSY(file2);
            }
            z2 = z3;
        } else if (!z || !deleteResolveEBUSY(file)) {
            z2 = false;
        }
        return z ? deleteResolveEBUSY(file) : z2;
    }

    private static boolean deleteResolveEBUSY(File file) {
        File file2 = new File(file.getAbsolutePath() + System.currentTimeMillis());
        file.renameTo(file2);
        return file2.delete();
    }

    public static boolean delete(String str, boolean z) {
        File file = new File(str);
        if (file.exists()) {
            return delete(file, z);
        }
        return false;
    }

    public static boolean delete(String str) {
        return delete(str, false);
    }

    public static boolean delete(File file) {
        return delete(file, false);
    }

    public static boolean copy(String str, String str2) {
        File file = new File(str);
        return file.exists() && copy(file, new File(str2));
    }

    public static boolean copy(File file, File file2) {
        try {
            LogUtils.verbose(String.format("copy %s to %s", file.getAbsolutePath(), file2.getAbsolutePath()));
            if (file.isFile()) {
                FileInputStream fileInputStream = new FileInputStream(file);
                FileOutputStream fileOutputStream = new FileOutputStream(file2);
                BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                byte[] bArr = new byte[8192];
                while (true) {
                    int read = bufferedInputStream.read(bArr);
                    if (read == -1) {
                        break;
                    }
                    bufferedOutputStream.write(bArr, 0, read);
                }
                bufferedInputStream.close();
                bufferedOutputStream.close();
            } else if (file.isDirectory()) {
                File[] listFiles = file.listFiles();
                file2.mkdirs();
                for (File file3 : listFiles) {
                    copy(file3.getAbsoluteFile(), new File(file2.getAbsoluteFile(), file3.getName()));
                }
            }
            return true;
        } catch (Exception e) {
            LogUtils.error(e);
            return false;
        }
    }

    public static boolean move(String str, String str2) {
        return move(new File(str), new File(str2));
    }

    public static boolean move(File file, File file2) {
        return rename(file, file2);
    }

    public static boolean rename(String str, String str2) {
        return rename(new File(str), new File(str2));
    }

    public static boolean rename(File file, File file2) {
        LogUtils.verbose(String.format("rename %s to %s", file.getAbsolutePath(), file2.getAbsolutePath()));
        return file.renameTo(file2);
    }

    public static String readText(String str, String str2) {
        LogUtils.verbose(String.format("read %s use %s", str, str2));
        try {
            byte[] readBytes = readBytes(str);
            return readBytes != null ? new String(readBytes, str2).trim() : "";
        } catch (UnsupportedEncodingException e) {
            LogUtils.warn(e);
            return "";
        }
    }

    public static String readText(String str) {
        return readText(str, "utf-8");
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r2v0, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r2v1 */
    /* JADX WARN: Type inference failed for: r2v3, types: [java.io.Closeable] */
    public static byte[] readBytes(String str) {
        FileInputStream fileInputStream;
        ?? r2 = "read %s";
        LogUtils.verbose(String.format("read %s", str));
        try {
            try {
                fileInputStream = new FileInputStream(str);
            } catch (IOException e) {
                e = e;
                fileInputStream = null;
            } catch (Throwable th) {
                th = th;
                r2 = 0;
                closeSilently(r2);
                throw th;
            }
            try {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                byte[] bArr = new byte[1024];
                while (true) {
                    int read = fileInputStream.read(bArr, 0, bArr.length);
                    if (read != -1) {
                        byteArrayOutputStream.write(bArr, 0, read);
                    } else {
                        byte[] byteArray = byteArrayOutputStream.toByteArray();
                        byteArrayOutputStream.close();
                        closeSilently(fileInputStream);
                        return byteArray;
                    }
                }
            } catch (IOException e2) {
                e = e2;
                LogUtils.warn(e);
                closeSilently(fileInputStream);
                return null;
            }
        } catch (Throwable th2) {
            th = th2;
            closeSilently(r2);
            throw th;
        }
    }

    public static boolean writeText(String str, String str2, String str3) {
        try {
            writeBytes(str, str2.getBytes(str3));
            return true;
        } catch (UnsupportedEncodingException e) {
            LogUtils.warn(e);
            return false;
        }
    }

    public static boolean writeText(String str, String str2) {
        return writeText(str, str2, "utf-8");
    }

    public static boolean writeBytes(String str, byte[] bArr) {
        LogUtils.verbose(String.format("write %s", str));
        File file = new File(str);
        FileOutputStream fileOutputStream = null;
        try {
            try {
                if (!file.exists()) {
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                }
                FileOutputStream fileOutputStream2 = new FileOutputStream(str);
                try {
                    fileOutputStream2.write(bArr);
                    closeSilently(fileOutputStream2);
                    return true;
                } catch (IOException e) {
                    e = e;
                    fileOutputStream = fileOutputStream2;
                    LogUtils.warn(e);
                    closeSilently(fileOutputStream);
                    return false;
                } catch (Throwable th) {
                    th = th;
                    fileOutputStream = fileOutputStream2;
                    closeSilently(fileOutputStream);
                    throw th;
                }
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (IOException e2) {
            e = e2;
        }
    }

    public static boolean appendText(String str, String str2) {
        LogUtils.verbose(String.format("append %s", str));
        File file = new File(str);
        FileWriter fileWriter = null;
        try {
            try {
                if (!file.exists()) {
                    file.createNewFile();
                }
                FileWriter fileWriter2 = new FileWriter(file, true);
                try {
                    fileWriter2.write(str2);
                    closeSilently(fileWriter2);
                    return true;
                } catch (IOException e) {
                    e = e;
                    fileWriter = fileWriter2;
                    LogUtils.warn(e);
                    closeSilently(fileWriter);
                    return false;
                } catch (Throwable th) {
                    th = th;
                    fileWriter = fileWriter2;
                    closeSilently(fileWriter);
                    throw th;
                }
            } catch (IOException e2) {
                e = e2;
            }
        } catch (Throwable th2) {
            th = th2;
        }
    }

    public static long getLength(String str) {
        File file = new File(str);
        if (file.isFile() && file.exists()) {
            return file.length();
        }
        return 0L;
    }

    public static String getName(String str) {
        if (str == null) {
            return "";
        }
        int lastIndexOf = str.lastIndexOf(47);
        if (lastIndexOf >= 0) {
            return str.substring(lastIndexOf + 1);
        }
        return String.valueOf(System.currentTimeMillis()) + FileAdapter.DIR_ROOT + getExtension(str);
    }

    public static String getNameExcludeExtension(String str) {
        try {
            String name = new File(str).getName();
            int lastIndexOf = name.lastIndexOf(FileAdapter.DIR_ROOT);
            return lastIndexOf != -1 ? name.substring(0, lastIndexOf) : name;
        } catch (Exception e) {
            LogUtils.warn(e);
            return "";
        }
    }

    public static String getSize(String str) {
        return ConvertUtils.toFileSizeString(getLength(str));
    }

    public static String getExtension(String str) {
        int lastIndexOf = str.lastIndexOf(46);
        return lastIndexOf >= 0 ? str.substring(lastIndexOf + 1) : "ext";
    }

    public static String getMimeType(String str) {
        String extension = getExtension(str);
        MimeTypeMap singleton = MimeTypeMap.getSingleton();
        String mimeTypeFromExtension = singleton.hasExtension(extension) ? singleton.getMimeTypeFromExtension(extension) : "*/*";
        LogUtils.verbose(str + ": " + mimeTypeFromExtension);
        return mimeTypeFromExtension;
    }

    public static String getDateTime(String str) {
        return getDateTime(str, "yyyy年MM月dd日HH:mm");
    }

    public static String getDateTime(String str, String str2) {
        return getDateTime(new File(str), str2);
    }

    public static String getDateTime(File file, String str) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(file.lastModified());
        return DateUtils.formatDate(calendar.getTime(), str);
    }

    public static int compareLastModified(String str, String str2) {
        long lastModified = new File(str).lastModified();
        long lastModified2 = new File(str2).lastModified();
        if (lastModified > lastModified2) {
            return 1;
        }
        return lastModified < lastModified2 ? -1 : 0;
    }

    public static boolean makeDirs(String str) {
        return makeDirs(new File(str));
    }

    public static boolean makeDirs(File file) {
        return file.mkdirs();
    }

    /* loaded from: classes.dex */
    public static class SortByExtension implements Comparator<File> {
        @Override // java.util.Comparator
        public int compare(File file, File file2) {
            if (file == null || file2 == null) {
                return file == null ? -1 : 1;
            } else if (file.isDirectory() && file2.isFile()) {
                return -1;
            } else {
                if (file.isFile() && file2.isDirectory()) {
                    return 1;
                }
                return file.getName().compareToIgnoreCase(file2.getName());
            }
        }
    }

    /* loaded from: classes.dex */
    public static class SortByName implements Comparator<File> {
        private boolean caseSensitive;

        public SortByName(boolean z) {
            this.caseSensitive = z;
        }

        public SortByName() {
            this.caseSensitive = false;
        }

        @Override // java.util.Comparator
        public int compare(File file, File file2) {
            if (file == null || file2 == null) {
                return file == null ? -1 : 1;
            } else if (file.isDirectory() && file2.isFile()) {
                return -1;
            } else {
                if (file.isFile() && file2.isDirectory()) {
                    return 1;
                }
                String name = file.getName();
                String name2 = file2.getName();
                if (this.caseSensitive) {
                    return name.compareTo(name2);
                }
                return name.compareToIgnoreCase(name2);
            }
        }
    }

    /* loaded from: classes.dex */
    public static class SortBySize implements Comparator<File> {
        @Override // java.util.Comparator
        public int compare(File file, File file2) {
            if (file == null || file2 == null) {
                return file == null ? -1 : 1;
            } else if (file.isDirectory() && file2.isFile()) {
                return -1;
            } else {
                return (!(file.isFile() && file2.isDirectory()) && file.length() < file2.length()) ? -1 : 1;
            }
        }
    }

    /* loaded from: classes.dex */
    public static class SortByTime implements Comparator<File> {
        @Override // java.util.Comparator
        public int compare(File file, File file2) {
            if (file == null || file2 == null) {
                return file == null ? -1 : 1;
            } else if (file.isDirectory() && file2.isFile()) {
                return -1;
            } else {
                return (!(file.isFile() && file2.isDirectory()) && file.lastModified() > file2.lastModified()) ? -1 : 1;
            }
        }
    }
}
